package com.kuyun.base.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kuyun.base.base.BaseController;
import com.kuyun.base.base.Page;
import com.kuyun.base.exception.KunYunException;
import com.kuyun.base.model.BaseUser;
import com.kuyun.base.model.HttpResult;
import com.kuyun.base.model.ValidFormResult;
import com.kuyun.base.util.Constants;
import com.kuyun.base.util.MD5Code;

/**
 * 用户管理控制器
 * 
 * @author 史梁辰
 * @date 2016-6-19下午10:40:26
 */
@Controller
@RequestMapping("/webback/BaseUser")
public class BaseUserController extends BaseController {
	/**
	 * 跳转用户编辑页面 根据id判断是新增还是修改
	 * 
	 * @author 史梁辰
	 * @date 2016-6-19下午10:50:50
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions({ "USER_ADD", "USER_UPDATE" })
	@RequestMapping("/toUserEdit.do")
	public String toUserEdit(String id, Model model) {
		model.addAttribute("dict", baseDictionaryService.getDicByParentCode(Constants.USER_TYPE));
		if (!StringUtils.isEmpty(id)) {// 如果id不为空则为跳转修改页面
			try {
				model.addAttribute("user", baseUserService.getUserById(id));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "user/user_edit";
	}

	/**
	 * 跳转用户管理页面
	 * 
	 * @author 史梁辰
	 * @date 2016-6-19下午10:50:50
	 * @param id
	 * @param model
	 * @return
	 */
	@RequiresPermissions("BASEUSER_LIST")
	@RequestMapping("/toUserList.do")
	public String toUserList() {
		return "user/user_list";
	}

	@ResponseBody
	@RequestMapping("/getUserList.do")
	@RequiresPermissions("BASEUSER_LIST")
	public Object getUserList(HttpServletRequest request, Page<BaseUser> page, BaseUser user) {
		Page<BaseUser> p = new Page<BaseUser>();
		try {
			p.setRows(baseUserService.findUser(page, user));
			p.setTotal(baseUserService.getUserConunt(user));
			if (p.getTotal() == 0) {
				return new Page<BaseUser>();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return p;
	}

	/**
	 * 新增或修改操作
	 * 
	 * @author 史梁辰
	 * @date 2016-6-19下午11:13:27
	 * @param User
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addOrUpdate.do")
	@RequiresPermissions({ "USER_ADD", "USER_UPDATE" })
	public ValidFormResult addOrUpdate(BaseUser User) {
		ValidFormResult result = new ValidFormResult();
		try {
			if (baseUserService.addOrUpdate(User)) {
				result.setStatus(ValidFormResult.Y);
			} else {
				result.setStatus(ValidFormResult.N);
			}
		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof KunYunException) {
				result.setInfo(e.getMessage());
			}
		}
		return result;
	}

	/**
	 * 启用或禁用
	 * 
	 * @author 史梁辰
	 * @date 2016-6-19下午11:13:47
	 * @param User
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/enOrDis.do")
	@RequiresPermissions({ "USER_ADD", "USER_UPDATE" })
	public HttpResult enabledOrDisabled(BaseUser user) {
		HttpResult result = new HttpResult();
		try {
			if (baseUserService.addOrUpdate(user)) {
				// result.setDesc("启用成功");
				result.setResultCode(Constants.RESULT_SUCCESS);
			}
		} catch (Exception e) {
			result.setDesc("服务器繁忙，请稍后再试");
			result.setResultCode(Constants.RESULT_FINAL);
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 删除操作
	 * 
	 * @author 史梁辰
	 * @date 2016-6-19下午11:13:47
	 * @param User
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	@RequiresPermissions("USER_DELETE")
	public HttpResult delete(String id) {
		try {
			return baseUserService.deleteUser(id);
		} catch (Exception e) {
			HttpResult result = new HttpResult();
			result.setDesc("服务器繁忙，请稍后再试");
			result.setResultCode(Constants.RESULT_FINAL);
			e.printStackTrace();
			return result;
		}
	}

	/**
	 * 用户名重复检查
	 * 
	 * @author 史梁辰
	 * @date 2016-7-10下午8:02:53
	 * @param param
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/checkUserName.do")
	@RequiresPermissions({ "USER_ADD", "USER_UPDATE" })
	public ValidFormResult checkUserName(String param, String id) {
		ValidFormResult result = new ValidFormResult();
		if (baseUserService.checkUserName(param, id) > 0) {
			result.setStatus(ValidFormResult.N);
			result.setInfo("用户名已存在");
		} else {
			result.setStatus(ValidFormResult.Y);
		}
		return result;
	}

	/**
	 * 跳转到修改密码页面
	 * 
	 * @author 唐靖
	 *
	 * @date 2016年12月25日下午12:28:15
	 *
	 * @return
	 */
	@RequestMapping("/toUpdatePasswordUI.do")
	public String toUpdatePasswordUI() {
		return "user/user_updatepassword";

	}

	/**
	 * 修改密码
	 * 
	 * @author 唐靖
	 *
	 * @date 2016年12月25日下午12:38:54
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updatePassword.do")
	public HttpResult updatePassword(String oldPassword, String newPassword, HttpServletRequest request) {
		HttpResult res = new HttpResult();
		BaseUser base = (BaseUser) request.getSession().getAttribute(Constants.USER_KEY);
		if (base.getPassword().equals(new MD5Code().getMD5ofStr(oldPassword.trim()))) {
			base.setPassword(newPassword.trim());
			try {
				baseUserService.addOrUpdate(base);
				request.getSession().invalidate();
				res.setResultCode(Constants.RESULT_SUCCESS);
			} catch (Exception e) {

				e.printStackTrace();
			}
		} else {
			res.setDesc("旧密码输入错误");
		}

		return res;

	}

}
